﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class NguoiDungDAO
    {
        public static List<NguoiDungDTO> getAll()
        {
            List<NguoiDungDTO> result = new List<NguoiDungDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.NGUOIDUNGs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                NguoiDungDTO dto = new NguoiDungDTO();

                dto.MaNguoiDung = ds.MaNguoiDung;
                dto.TenNguoiDung = ds.TenNguoiDung;
                dto.MatKhau = ds.MatKhau;
                dto.Email = ds.Email;
                dto.DienThoai = ds.DienThoai;
                dto.DiaChi = ds.DiaChi;
                dto.MaKichHoatTaiKhoan = ds.MaKichHoatTaiKhoan;
                dto.TinhTrangKichHoatTaiKhoan = ds.TinhTrangKichHoatTaiKhoan;
                dto.ThoiGianDangKy = ds.ThoiGianDangKy.Value;
                dto.ThoiGianHetHan = ds.ThoiGianHetHan.Value;
                dto.MaLoaiNguoiDung = ds.MaLoaiNguoiDung.Value;

                //////////////////Sửa lại Mã giao diện sau
                dto.MaGiaoDien = 1;

                result.Add(dto);
            }

            return result;
        }

        public static NguoiDungDTO getAlongCode(int maNguoiDung)
        {
            NguoiDungDTO result = new NguoiDungDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.NGUOIDUNGs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaNguoiDung == maNguoiDung).Single();

            result.MaNguoiDung = query.MaNguoiDung;
            result.TenNguoiDung = query.TenNguoiDung;
            result.MatKhau = query.MatKhau;
            result.Email = query.Email;
            result.DienThoai = query.DienThoai;
            result.DiaChi = query.DiaChi;
            result.MaKichHoatTaiKhoan = query.MaKichHoatTaiKhoan;
            result.TinhTrangKichHoatTaiKhoan = query.TinhTrangKichHoatTaiKhoan;
            result.ThoiGianDangKy = query.ThoiGianDangKy.Value;
            result.ThoiGianHetHan = query.ThoiGianHetHan.Value;
            result.MaLoaiNguoiDung = query.MaLoaiNguoiDung.Value;
            result.MaGiaoDien = query.MaGiaoDien.Value;

            return result;
        }

        public static NguoiDungDTO layNguoiDungTheoTen(string strUsername)
        {
            List<NguoiDungDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].TenNguoiDung.Trim().ToLower().CompareTo(strUsername.Trim().ToLower()) == 0)
                {
                    return DS[i];
                }
            }

            return null;
        }

        public static NguoiDungDTO layNguoiDungTheoEmail(string strEmail)
        {
            List<NguoiDungDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].Email.Trim().ToLower().CompareTo(strEmail.Trim().ToLower()) == 0)
                {
                    return DS[i];
                }
            }

            return null;
        }

        public static bool kiemTraEmailTrung(string strEmail)
        {
            List<NguoiDungDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].Email.ToLower().CompareTo(strEmail.ToLower()) == 0)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool kiemTraTenTrung(string strTen)
        {
            List<NguoiDungDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].TenNguoiDung.ToLower().CompareTo(strTen.ToLower()) == 0)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool Insert(NguoiDungDTO _insert)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            NGUOIDUNG nguoidung = new NGUOIDUNG();
            //nguoidung.MaNguoiDung = _insert.MaNguoiDung;
            nguoidung.TenNguoiDung = _insert.TenNguoiDung;
            nguoidung.MatKhau = _insert.MatKhau;
            nguoidung.Email = _insert.Email;
            nguoidung.DienThoai = _insert.DienThoai;
            nguoidung.DiaChi = _insert.DiaChi;
            nguoidung.MaKichHoatTaiKhoan = _insert.MaKichHoatTaiKhoan;
            nguoidung.TinhTrangKichHoatTaiKhoan = _insert.TinhTrangKichHoatTaiKhoan;
            nguoidung.ThoiGianDangKy = _insert.ThoiGianDangKy;
            nguoidung.ThoiGianHetHan = _insert.ThoiGianHetHan;
            nguoidung.MaLoaiNguoiDung = _insert.MaLoaiNguoiDung;
            nguoidung.MaGiaoDien = _insert.MaGiaoDien;
            nguoidung.Deleted = _insert.Deleted;
            
            try
            {
                dbRaoVat.NGUOIDUNGs.InsertOnSubmit(nguoidung);
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public static bool InsertTemplate(NguoiDungDTO _insert)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            NGUOIDUNG nguoidung = new NGUOIDUNG();
            //nguoidung.MaNguoiDung = _insert.MaNguoiDung;
            /*nguoidung.TenNguoiDung = _insert.TenNguoiDung;
            nguoidung.MatKhau = _insert.MatKhau;
            nguoidung.Email = _insert.Email;
            nguoidung.DienThoai = _insert.DienThoai;
            nguoidung.DiaChi = _insert.DiaChi;
            nguoidung.MaKichHoatTaiKhoan = _insert.MaKichHoatTaiKhoan;
            nguoidung.TinhTrangKichHoatTaiKhoan = _insert.TinhTrangKichHoatTaiKhoan;
            nguoidung.ThoiGianDangKy = _insert.ThoiGianDangKy;
            nguoidung.ThoiGianHetHan = _insert.ThoiGianHetHan;
            nguoidung.MaLoaiNguoiDung = _insert.MaLoaiNguoiDung;
            //nguoidung.MaGiaoDien = _insert.MaGiaoDien;*/
            nguoidung.TenNguoiDung = "";
            nguoidung.MaLoaiNguoiDung = 1;
            nguoidung.Deleted = 1;

            try
            {
                dbRaoVat.NGUOIDUNGs.InsertOnSubmit(nguoidung);
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public static void Delete(int maNguoiDung)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            NGUOIDUNG _delete = (from n in dbRaoVat.NGUOIDUNGs
                                 where n.MaNguoiDung == maNguoiDung && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static bool Update(NguoiDungDTO dto)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            NGUOIDUNG _update = (from n in dbRaoVat.NGUOIDUNGs
                                 where n.MaNguoiDung == dto.MaNguoiDung
                                 select n).Single();

            _update.TenNguoiDung = dto.TenNguoiDung;
            _update.MatKhau = dto.MatKhau;
            _update.Email = dto.Email;
            _update.DienThoai = dto.DienThoai;
            _update.DiaChi = dto.DiaChi;
            _update.MaKichHoatTaiKhoan = dto.MaKichHoatTaiKhoan;
            _update.TinhTrangKichHoatTaiKhoan = dto.TinhTrangKichHoatTaiKhoan;
            _update.ThoiGianDangKy = dto.ThoiGianDangKy;
            _update.ThoiGianHetHan = dto.ThoiGianHetHan;
            _update.MaLoaiNguoiDung = dto.MaLoaiNguoiDung;
            //_update.MaGiaoDien = dto.MaGiaoDien;
            _update.Deleted = dto.Deleted;

            try
            {
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public static NguoiDungDTO getNguoiDungTheoMa(int maNguoiDung)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.NGUOIDUNGs.Where(dm =>
                dm.MaNguoiDung == maNguoiDung).Single();

            return new NguoiDungDTO(maNguoiDung, query.TenNguoiDung,query.MatKhau, query.Email,
                query.DienThoai, query.DiaChi, query.MaKichHoatTaiKhoan, query.TinhTrangKichHoatTaiKhoan, 
                query.ThoiGianDangKy.Value,  query.ThoiGianHetHan.Value, query.MaLoaiNguoiDung.Value,
                query.MaGiaoDien.Value, query.Deleted.Value);
        }
        
    }
}
